package action;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.json.JSONArray;
import org.json.JSONObject;

import po.tree_one;
import po.tree_three;
import po.tree_two;
import service.PeizhiService;

import com.google.gson.Gson;
import com.opensymphony.xwork2.ActionSupport;

public class PeizhiAction extends ActionSupport{
	
	
	private String jsonData;
	private PeizhiService peizhiservice;
	private String add_peizhi;
	private List<tree_one> tree1List;
	private List<tree_two> tree2List;
	private List<tree_three> tree3List;
	
	public List<tree_one> getTree1List() {
		return tree1List;
	}


	public void setTree1List(List<tree_one> tree1List) {
		this.tree1List = tree1List;
	}


	public List<tree_two> getTree2List() {
		return tree2List;
	}


	public void setTree2List(List<tree_two> tree2List) {
		this.tree2List = tree2List;
	}


	public List<tree_three> getTree3List() {
		return tree3List;
	}


	public void setTree3List(List<tree_three> tree3List) {
		this.tree3List = tree3List;
	}


	public void setAdd_peizhi(String add_peizhi) {
		this.add_peizhi = add_peizhi;
	}


	public String getAdd_peizhi() {
		return add_peizhi;
	}


	public void setPeizhiservice(PeizhiService peizhiservice) {
		this.peizhiservice = peizhiservice;
	}


	public String getJsonData() {
		return jsonData;
	}


	public void setJsonData(String jsonData) {
		this.jsonData = jsonData;
	}


	
	
	public String addPeizhi() throws Exception{
		
		Set<tree_three> tree_threes=new HashSet<tree_three>();//所有的三级   
        Set<tree_two> tree_twos=new HashSet<tree_two>();//所有的二级
        Set<tree_one> tree_ones=new HashSet<tree_one>();//所有的一级
   
		JSONArray jsonArray=new JSONArray(jsonData);//所有一级
        for(int i=0;i<jsonArray.length();i++){
        	
            JSONObject tree_one=jsonArray.getJSONObject(i);//某个一级
            tree_one tree1=new tree_one();
            tree1.setTree_one_id(tree_one.getInt("tree_one_id"));
            tree1.setTree_one_name(tree_one.getString("tree_one_name"));
            tree1.setTree_one_href(tree_one.getString("tree_one_href"));  
            JSONArray one_to_two=tree_one.getJSONArray("one_to_two");//所有二级
            List<tree_two> tree2list=new ArrayList<tree_two>();//某个一级的所有二级
           
            if(!one_to_two.equals("[]")){
            	
            	for(int j=0;j<one_to_two.length();j++){
            		JSONObject tree_two=one_to_two.getJSONObject(j);//某个二级
            		tree_two tree2=new tree_two();
            		tree2.setTree_two_id(tree_two.getInt("tree_two_id"));
            		tree2.setTree_two_name(tree_two.getString("tree_two_name"));
            		tree2.setTree_two_href(tree_two.getString("tree_two_href"));
            		tree2.setTree_one(tree1);
            		JSONArray two_to_three=tree_two.getJSONArray("two_to_three");//所有三级
            		List<tree_three> tree3list=new ArrayList<tree_three>();//某个二级的所有三级
            		
            		if(!two_to_three.equals("[]")){
            		    
            			for(int k=0;k<two_to_three.length();k++){
                			tree_three tree3=new tree_three();
                			tree3.setTree_three_id(two_to_three.getJSONObject(k).getInt("tree_three_id"));
                			tree3.setTree_three_name(two_to_three.getJSONObject(k).getString("tree_three_name"));
                			tree3.setTree_three_href(two_to_three.getJSONObject(k).getString("tree_three_href"));
                			tree3.setTree_two(tree2);
                			tree_threes.add(tree3);
                			tree3list.add(tree3);
                		}
            		}
            		
            		
            		tree2.setTwo_to_three(tree3list);
            		tree_twos.add(tree2);
            		tree2list.add(tree2);
            		
            	}
                    
             tree1.setOne_to_two(tree2list);   
             tree_ones.add(tree1);  
                   
             
            }
        	
        }
        
        this.add_peizhi=this.peizhiservice.addPeizhi(tree_ones,tree_twos,tree_threes);
	  
		if(this.getAdd_peizhi().equals("add_success")){
			return "addSuccess";
		}else{
			return "addError";
		}
	}
	
	
	public String loadPeizhi() throws Exception{
		
		this.tree1List=this.peizhiservice.list_tree1();
		this.tree2List=this.peizhiservice.list_tree2();
		this.tree3List=this.peizhiservice.list_tree3();			
		List<tree_one> list1=new ArrayList<tree_one>();//所有的一级
		if(this.tree1List!=null){
			for(int i=0;i<this.tree1List.size();i++){
				tree_one tree_one=new tree_one();
				tree_one tree1=this.tree1List.get(i);
				tree_one.setTree_one_id(tree1.getTree_one_id());
				tree_one.setTree_one_name(tree1.getTree_one_name());
				tree_one.setTree_one_href(tree1.getTree_one_href());
				
				List<tree_two> list2=new ArrayList<tree_two>();//某个一级的所有二级
				
				if(this.tree2List!=null){	
				
					for(int j=0;j<this.tree2List.size();j++){
						tree_two tree_two=new tree_two();
						tree_two tree2=this.tree2List.get(j);
						if(tree2.getTree_one().getTree_one_id()==tree_one.getTree_one_id()){//判断某个二级是否一级的子目录
							tree_two.setTree_two_id(tree2.getTree_two_id());
							tree_two.setTree_two_name(tree2.getTree_two_name());
							tree_two.setTree_two_href(tree2.getTree_two_href());
							List<tree_three> list3=new ArrayList<tree_three>();//某个二级的所有三级
						  if(this.tree3List!=null){	
								for(int k=0;k<this.tree3List.size();k++){
									tree_three tree_three=new tree_three();
									tree_three tree3=this.tree3List.get(k);
									if(tree3.getTree_two().getTree_two_id()==tree_two.getTree_two_id()){//判断某个三级是否二级的子目录
										tree_three.setTree_three_id(tree3.getTree_three_id());
										tree_three.setTree_three_name(tree3.getTree_three_name());
										tree_three.setTree_three_href(tree3.getTree_three_href());
										list3.add(tree_three);
									}
								}
								
								tree_two.setTwo_to_three(list3);
								list2.add(tree_two);
						    }
						}
						
						
					}
				}
				
				tree_one.setOne_to_two(list2);
				list1.add(tree_one);
			}
		}
		Gson gson=new Gson();
		String result=gson.toJson(list1);
		//System.out.println(result);
	    HttpServletResponse response = ServletActionContext.getResponse();
		
		response.setContentType("application/json; charset=utf-8");
		response.setHeader("cache-control", "no-cache"); 
		
		PrintWriter out = response.getWriter();
		//System.out.println(result);
		out.print(result);
		
		out.flush();
		out.close();
		return null;
	}

}
